package com.theobserver.theobserver;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.GetCallback;

import java.util.ArrayList;

/**
 * Created by wssxk on 7/8/17.
 */

public class NewsXXPaperFragment extends Fragment {

    Context mContext = null;
    AVUser cur_user = null;

    public View onCreateView(LayoutInflater inflater, ViewGroup viewgroup, Bundle savedInstance)
    {
        View v = inflater.inflate(R.layout.news_list_without_catalog_main_page, null);

        TextView collect_title = (TextView) v.findViewById(R.id.textview_withoutca_main_title);
        collect_title.setText("电脑报新闻");
        View add_more = v.findViewById(R.id.relativelayout_news_addmore);
        add_more.setVisibility(View.GONE);

        ListView lv = (ListView) v.findViewById(R.id.listview_news);
        final NewsListAdapter adapter = new NewsListAdapter(mContext);
        lv.setAdapter(adapter);

        final DrawerLayout drawer = ((FragmentActivity) mContext).findViewById(R.id.main_page_drawer);
        ImageView column_slider = v.findViewById(R.id.imageview_withoutca_main_slider);
        column_slider.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawer.openDrawer(Gravity.LEFT);
            }
        });

        final SwipeRefreshLayout sl = (SwipeRefreshLayout) v.findViewById(R.id.swiperefreshlayout_news);
        sl.setRefreshing(true);

        cur_user = new AVUser();
        cur_user.setObjectId("596570faa22b9d006a68cfe6");
        cur_user.fetchInBackground(new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                if(e == null)
                {
                    News.getSendedNews(cur_user, new NewsCallBack() {
                        @Override
                        public void getNews(ArrayList<News> news, int state, AVException e) {
                            sl.setRefreshing(false);
                            if(news != null && news.size() != 0)
                            {
                                //sl.setRefreshing(false);
                                adapter.data.clear();
                                adapter.data.addAll(news);
                                adapter.notifyDataSetChanged();
                            }
                            else
                            {
                                adapter.data.clear();
                                adapter.notifyDataSetChanged();
                            }

                        }
                    });

                }
            }
        });

        sl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                News.getSendedNews(cur_user, new NewsCallBack() {
                    @Override
                    public void getNews(ArrayList<News> news, int state, AVException e) {
                        sl.setRefreshing(false);
                        if(news != null && news.size() != 0)
                        {
                            adapter.data.clear();
                            adapter.data.addAll(news);
                            adapter.notifyDataSetChanged();
                        }
                        else
                        {
                            adapter.data.clear();
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent news_detail_intent = new Intent(mContext, NewsDetailActivity.class);
                //news_detail_intent.putExtras();
                news_detail_intent.putExtra("objectId",
                        ((NewsItemFrameLayout) view.findViewById(R.id.framelayout_news_item)).news.cur_news.getObjectId());
                startActivity(news_detail_intent);
            }
        });


        return v;
    }

    public void init(Context context)
    {
        mContext = context;
    }


}
